package com.beerq.view;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.beerq.R;
import com.beerq.util.Constants;
import com.beerq.util.ViewPaperUtil;
import com.beerq.util.WebViewUtil;

import java.util.ArrayList;
import java.util.List;


public class FirstFragment extends Fragment implements View.OnClickListener {

    private ViewPager mViewPager;
    private List<View> mListViews;
    private Activity mActivity;

    private LinearLayout mLinSuggest;
    private LinearLayout mLinVProduct;
    private LinearLayout mLinVCountry;
    private LinearLayout mLinVPackage;
    private LinearLayout mLinVSecondBuy;





    //好啤酒的返回到顶部
    private View mLin_back_to_top;

    public static FirstFragment newInstance(String param1, String param2) {
        FirstFragment fragment = new FirstFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;


    }

    @Override
    public void onResume() {
        super.onResume();
        ProductJsManager.isShowBackToTop((WebView) mListViews.get(0));

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }


        mActivity = getActivity();

    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();


        new ViewPaperUtil(getActivity(),mViewPager).bind(mListViews, new ViewPaperUtil.IGetCurrentSelectIndex() {
            @Override
            public void getSelectIndex(int pos) {
                ProductJsManager.isShowBackToTop((WebView) mListViews.get(pos));
            }
        });


        mViewPager.setCurrentItem(2);
    }



    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            ProductJsManager.isShowBackToTop((WebView) mListViews.get(mViewPager.getCurrentItem()));
            if(Constants.userInfo!=null && mListViews.get(0)!=null){
                ProductJsManager.setUserInfo((WebView) mListViews.get(0),Constants.userInfo.getAppUserId());
            }
        }else {
            mLin_back_to_top.setVisibility(View.GONE);
        }
    }

    private void initView() {
        mViewPager = (ViewPager) (mActivity.findViewById(R.id.id_viewpager));
        mLin_back_to_top=(mActivity.findViewById(R.id.lin_back_to_top));
        mListViews = new ArrayList<View>();
        addView( Constants.domain+"/#/goods",0);
        addView(Constants.domain+"#/list",1);
        addView(Constants.domain+"#/recommend",2);
        addView(Constants.domain+"#/package",3);
        addView(Constants.domain+"#/sales",4);

        mLinSuggest = (LinearLayout) (mActivity.findViewById(R.id.lin_suggest));
        mLinVProduct = (LinearLayout) (mActivity.findViewById(R.id.lin_product));
        mLinVCountry = (LinearLayout) (mActivity.findViewById(R.id.lin_country));
        mLinVPackage = (LinearLayout) (mActivity.findViewById(R.id.lin_package));
        mLinVSecondBuy = (LinearLayout) (mActivity.findViewById(R.id.lin_second_buy));
        mLinSuggest.setOnClickListener(this);
        mLinVProduct.setOnClickListener(this);
        mLinVCountry.setOnClickListener(this);
        mLinVPackage.setOnClickListener(this);
        mLinVSecondBuy.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int vId=v.getId();

        switch (vId){
            case R.id.lin_suggest:
                mViewPager.setCurrentItem(2);
                break;
            case R.id.lin_product:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.lin_country:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.lin_package:
                mViewPager.setCurrentItem(3);
                break;
            case R.id.lin_second_buy:
                mViewPager.setCurrentItem(4);
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

    private void addView(String url,int index) {
        WebView webView = new WebView(mActivity);
        ProductJsManager productJsManager=new ProductJsManager(getActivity(),webView);
        productJsManager.setLin_back_to_top(mLin_back_to_top);
        webView.addJavascriptInterface(productJsManager, "android");

        if(index==0){

//           WebView webView111= (WebView)(getActivity().findViewById(R.id.webview_test));
//           webView111.destroy();
//           webView111.setVisibility(View.GONE);
           Constants.firstWebView=webView;
        }

        webView.setWebChromeClient(
                new WebChromeClient() {
                    @Override
                    public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {

                        if("1".equals(message)){
                            mLin_back_to_top.setVisibility(View.VISIBLE);
                        }else{
                            mLin_back_to_top.setVisibility(View.GONE);
                        }

                        result.confirm();
                        return true;
                    }}
        );

        WebViewUtil.initWebView(getActivity(),webView);webView.loadUrl(url);
        mListViews.add(webView);

        mLin_back_to_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebView temp=(WebView) mListViews.get(mViewPager.getCurrentItem());
                temp.loadUrl("javascript:icangotop()");
            }
        });
    }
}